<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    用户登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip hide">输入任意用户名和密码即可</p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import util from '../libs/util';
import storage from '../libs/storage';
import config from '../../build/config';
import md5 from 'md5';

export default {
    data () {
        return {
            form: {
                userName: '',
                password: ''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' },
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    /**
                     * 开始授权登录
                     */
                    var form_params = {
                        'grant_type': 'password',
                        'client_id': config.appId,
                        'client_secret': config.appSecret,
                        'username': this.form.userName,
                        'password': this.form.password,
                        'scope': 'user-info',
                        'backend': 'yes'
                    };

                    util.ajax.post('/oauth/token', form_params)
                        .then((resp) => {
                            storage.set('tokens', resp.data);
                            this.$store.dispatch('setTokens', resp.data);

                            this.$store.commit('setAvator', 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg');

                            util.api().get('/admin/api/user')
                                .then((resp) => {
                                    if (resp.data.status == true) {
                                        this.$Notice.success({ title: '登录成功', duration: 1.5 });

                                        storage.set('lockPassword', md5(this.form.password));
                                        Cookies.set('user', resp.data.data);
                                        storage.set('user', resp.data.data);

                                        util.ajax.post('login', {name: this.form.userName, password: this.form.password});

                                        this.$store.dispatch('setUser', resp.data.data);
                                        // 登录成功进入首页
                                        this.$router.push({
                                            name: 'system-my-profiles'
                                        });
                                    } else {
                                        this.$Notice.error({ title: resp.data.message, duration: 1.5 });
                                    }
                                })
                                .catch((e) => {
                                    util.handleApiException(e, this, '获取用户信息失败');
                                });
                        })
                        .catch((e) => {
                            util.handleApiException(e, this, '登录失败');
                        });
                }
            });
        }
    }
};
</script>

<style>

</style>
